package org.nanhill.support.jmx.dwjmxservice;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

class ClickPanelModel {
	int val = 0;

	ClickPanel myView;

	public ClickPanelModel(ClickPanel inView) {
		myView = inView;
	}

	public void setVal(int inVal) {
		val = inVal;
		myView.notifyUpdate();
	}

	public void incVal() {
		++val;
		myView.notifyUpdate();
	}

	public void decVal() {
		--val;
		myView.notifyUpdate();
	}

	public int getVal() {
		return val;
	}
}

class ClickPanelController {

	public ClickPanelModel myModel;

	public ClickPanelController(ClickPanelModel inModel, ClickPanel inPanel) {
		myModel = inModel;
	}

	public void onIncButtonClicked() {
		myModel.incVal();
	}

	public void onDecButtonClicked() {
		myModel.decVal();
	}
}

public class ClickPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6895077701644372233L;

	ClickPanelModel myModel = null;

	ClickPanelController myController = null;

	JLabel dispLabel = new JLabel();

	JPanel buttonsPanel = new JPanel();

	JButton incButton = new JButton();

	JButton decButton = new JButton();

	BorderLayout borderLayout = new BorderLayout();

	GridLayout gridLayout = new GridLayout();

	public ClickPanel() {
		// enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		try {
			guiInit();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void initController() {
		myModel = new ClickPanelModel(this);
		myController = new ClickPanelController(myModel, this);
	}

	private void guiInit() throws Exception {

		this.setLayout(borderLayout);
		this.setPreferredSize(new Dimension(100, 200));
		dispLabel.setBackground(Color.white);
		dispLabel.setFont(new java.awt.Font("Serif", 1, 89));
		dispLabel.setMaximumSize(new Dimension(500, 400));
		dispLabel.setOpaque(true);
		dispLabel.setToolTipText("Current Reading");
		dispLabel.setHorizontalAlignment(SwingConstants.CENTER);
		dispLabel.setText("0");
		buttonsPanel.setPreferredSize(new Dimension(50, 50));

		incButton.setFont(new java.awt.Font("SansSerif", 1, 22));
		incButton.setText("+");
		incButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				incButtonClicked(e);
			}
		});
		decButton.setFont(new java.awt.Font("SansSerif", 1, 22));
		decButton.setText("-");
		decButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				decButtonClicked(e);
			}
		});

		gridLayout.setColumns(1);
		gridLayout.setRows(2);
		gridLayout.setVgap(1);
		buttonsPanel.setLayout(gridLayout);
		this.add(dispLabel, BorderLayout.CENTER);
		this.add(buttonsPanel, BorderLayout.EAST);
		buttonsPanel.add(incButton, null);
		buttonsPanel.add(decButton, null);
	}

	void incButtonClicked(ActionEvent e) {
		myController.onIncButtonClicked();
	}

	void decButtonClicked(ActionEvent e) {
		myController.onDecButtonClicked();
	}

	public void notifyUpdate() {
		dispLabel.setText("" + myModel.getVal());
	}
}